# config/celery_config.py

from .settings import settings

class CeleryConfig:
    """Celery配置类"""
    
    # 基础配置
    BROKER_URL = "redis://10.3.244.23:36379/6"
    RESULT_BACKEND = "redis://10.3.244.23:36379/6"
    
    # 序列化配置
    TASK_SERIALIZER = "json"
    RESULT_SERIALIZER = "json"
    ACCEPT_CONTENT = ["json"]
    
    # 时区配置
    TIMEZONE = "Asia/Shanghai"
    ENABLE_UTC = True
    
    # 任务配置
    TASK_TRACK_STARTED = True
    TASK_TIME_LIMIT = 30 * 60  # 30分钟超时
    TASK_SOFT_TIME_LIMIT = 25 * 60  # 25分钟软超时
    
    # Worker配置
    WORKER_PREFETCH_MULTIPLIER = 1
    WORKER_MAX_TASKS_PER_CHILD = 1000
    
    # 结果配置
    RESULT_EXPIRES = 3600  # 1小时过期
    
    # 路由配置
    TASK_ROUTES = {
        'tasks.file_tasks.*': {'queue': 'file_processing'},
        'tasks.vector_tasks.*': {'queue': 'vector_processing'},
        'tasks.batch_tasks.*': {'queue': 'batch_processing'},
    }
    
    # 队列配置
    TASK_DEFAULT_QUEUE = 'default'
    TASK_DEFAULT_EXCHANGE = 'default'
    TASK_DEFAULT_ROUTING_KEY = 'default'
    
    # 监控配置
    FLOWER_PORT = 5555
    FLOWER_HOST = "0.0.0.0"
    
    @classmethod
    def get_config(cls):
        """获取Celery配置字典"""
        return {
            'broker_url': cls.BROKER_URL,
            'result_backend': cls.RESULT_BACKEND,
            'task_serializer': cls.TASK_SERIALIZER,
            'result_serializer': cls.RESULT_SERIALIZER,
            'accept_content': cls.ACCEPT_CONTENT,
            'timezone': cls.TIMEZONE,
            'enable_utc': cls.ENABLE_UTC,
            'task_track_started': cls.TASK_TRACK_STARTED,
            'task_time_limit': cls.TASK_TIME_LIMIT,
            'task_soft_time_limit': cls.TASK_SOFT_TIME_LIMIT,
            'worker_prefetch_multiplier': cls.WORKER_PREFETCH_MULTIPLIER,
            'worker_max_tasks_per_child': cls.WORKER_MAX_TASKS_PER_CHILD,
            'result_expires': cls.RESULT_EXPIRES,
            'task_routes': cls.TASK_ROUTES,
            'task_default_queue': cls.TASK_DEFAULT_QUEUE,
            'task_default_exchange': cls.TASK_DEFAULT_EXCHANGE,
            'task_default_routing_key': cls.TASK_DEFAULT_ROUTING_KEY,
        } 